package com.example.api;

import com.example.exception.NotAllowException;
import com.example.pojo.TbBook;
import com.example.pojo.TbBookBase;
import com.example.service.TbBookBaseService;
import com.example.service.TbBookService;
import com.example.util.Response;
import com.example.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2022/12/14
 * Description: url:http://localhost:8080/bookInfo/books
 */
@RestController
@RequestMapping("/bookInfo")
@Api(tags = "书籍信息详情")
public class AdminBooksInfoAPI {
    @Resource
    private TbBookBaseService tbBookBaseService;

    //查询所有书籍销售相关信息
    @GetMapping("/books")
    @ApiOperation("查询所有书籍详细信息")
    public ResponseResult<List<TbBookBase>> getAllBook(@RequestHeader String token) {
        List<TbBookBase> bookList = tbBookBaseService.list();
//        return bookList;
        System.out.println(bookList);
        return Response.createOkResp(bookList);
    }

    @GetMapping("/booksByCategory/{cid}")
    @ApiOperation("根据书籍分类查询详细信息")
    public ResponseResult<List<TbBookBase>> getbyCategoryID(@PathVariable("cid") Long cid, @RequestHeader String token) {
        List<TbBookBase> tbBook = tbBookBaseService.selectByCategoryId(cid);
        return Response.createOkResp(tbBook);
    }

    @GetMapping("/booksByName/{bookName}")
    @ApiOperation("根据书名模糊查询")
    public ResponseResult<List<TbBookBase>> getbyBookName(@PathVariable("bookName") String name, @RequestHeader String token) {
        List<TbBookBase> bookList = tbBookBaseService.selectByName(name);
        return Response.createOkResp(bookList);
    }

    @GetMapping("/booksByWriter/{writerNmae}")
    @ApiOperation("根据作者名模糊查询")
    public ResponseResult<List<TbBookBase>> getbyBookWriter(@PathVariable("writerNmae") String writer, @RequestHeader String token) {
        List<TbBookBase> bookList = tbBookBaseService.selectByWriter(writer);
        return Response.createOkResp(bookList);
    }

//    @PostMapping("/books")
//    @ApiOperation("添加书籍详细信息")
//    public ResponseResult<TbBookBase> addBook(TbBookBase tbBook) throws NotAllowException {
////        TbBook tbBookTemp = tbBookService.selectById(tbBook.getBookId());
////        //找不到id
////        if (tbBookTemp.equals(null)) throw new NotAllowException();
//        int num = tbBookBaseService.add(tbBook);
//        System.out.println(num);
//        return Response.createOkResp("添加成功");
//    }

    @PutMapping("/books")
    @ApiOperation("修改书籍详细信息")
    public ResponseResult<Map> update(@Valid TbBookBase tbBook, @RequestHeader String token) {
        int num = tbBookBaseService.update(tbBook);
        System.out.println(num);
        return Response.createOkResp("修改成功");
    }


}
